// scripts/build-api-doc.mjs
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';

const tempDir = path.resolve('./temp');       // 所有 .api.json 放这里
const outputRoot = path.resolve('./docs/api'); // 输出根目录

// 读取 temp 下所有 .api.json 文件
const files = fs.readdirSync(tempDir).filter(f => f.endsWith('.api.json'));

for (const file of files) {
  // 根据文件名作为包名分类输出
  const pkgName = path.basename(file, '.api.json');
  const pkgOutput = path.join(outputRoot, pkgName);
  fs.mkdirSync(pkgOutput, { recursive: true });

  console.log(`Generating Markdown for package: ${pkgName}`);
  execSync(`npx api-documenter markdown -i "${path.join(tempDir, file)}" -o "${pkgOutput}"`, {
    stdio: 'inherit'
  });
}

console.log('All package API docs generated.');
